/*
	DataSegment.m
        Copyright 2008 Laura Dickey.  All rights reserved.

        This file is part of BrushViewQL.

        BrushViewQL is free software; you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation; either version 2 of the License, or
        (at your option) any later version.

        BrushViewQL is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with BrushViewQL; if not, write to the Free Software
        Foundation, Inc., 59 Temple Place, Suite 330,
        Boston, MA  02111-1307  USA

        You may contact the author at: brushviewql@gmail.com
*/

#import "DataSegment.h"
#import "BrushFile.h"


@implementation DataSegment

- (void)setSubVersion:(int)vers
{
	subversion = vers;
}

- (void)setTag:(NSData *)data
{
	[data getBytes:tag];
	tag[4] = 0;
}

- (void)setTagName:(NSData *)data
{
	[data getBytes:tagname];
	tagname[4] = 0;
}

- (void)setLength:(unsigned long)theLength
{
	length = theLength;
}

- (void)setBytes:(NSData *)data
{
	bytes = [NSData dataWithData:data];
	[bytes retain];
}

#define READ_LONG(val) \
	val = *(unsigned long *)brushData;\
	val = CFSwapInt32BigToHost(val);\
	brushData += sizeof(val);
	
#define READ_SHORT(val) \
	val = *(unsigned short *)brushData;\
	val = CFSwapInt16BigToHost(val);\
	brushData += sizeof(val);

- (id)loadBrushes
{
	unsigned char *brushData, *brushDataEnd, *nextBrush;
	long	brushSize;
	long	nextBrushOffset;
	long	top, left, bottom, right;
	long	width, height;
	short	depth;
	char	compress;
	int	padding;
	NSImage	*theImage = nil;
	NSMutableArray	*theImages;
	
	brushData = (unsigned char *)[bytes bytes];
	brushDataEnd = brushData + [bytes length];
	theImages = [[[NSMutableArray alloc] initWithCapacity:10] retain];
	
	while (brushData < brushDataEnd) {
		brushSize = *(long *)brushData;
		brushSize = CFSwapInt32BigToHost(brushSize);
		nextBrushOffset = brushSize;
		padding = 0;
		while ((nextBrushOffset % 4) != 0) {
			++nextBrushOffset;
			++padding;
		}
		brushData += sizeof(brushSize);
		nextBrush = brushData + nextBrushOffset;
		if (subversion == 1) {
			brushData += 47;
		} else if (subversion == 2) {
			brushData += 301;
		} else {
			NSAssert(0, @"Bad subversion!");
		}
		READ_LONG(top);
		READ_LONG(left);
		READ_LONG(bottom);
		READ_LONG(right);
		//BRLog(@"top = %d, left = %d, bottom = %d, right = %d",
			//top, left, bottom, right);
		depth = *(short *)brushData;
		depth = CFSwapInt16BigToHost(depth);
		brushData +=2;
		compress = *brushData++;
		//BRLog(@"depth = %d, compress = %d", depth, compress);
		width = right - left;
		height = bottom - top;
		//BRLog(@"width = %d, height = %d", width, height);
		theImage = [BrushFile createImage:&brushData
			width:width height:height depth:depth
			compressed:compress];
		[theImages insertObject:theImage atIndex:0];
		while (padding--) {
			++brushData;
		}
		brushData = nextBrush; 
	}
	//BRLog(@"Read in %d brushes", [theImages count]);
	return theImages;
}

@end
